const path = require('path')
const webpack = require('webpack')
const createThemeColorReplacerPlugin = require('./config/plugin.config')

function resolve(dir) {
  return path.join(__dirname, dir)
}

const isProd = process.env.NODE_ENV === 'production'

const assetsCDN = {
  // webpack build externals
  externals: {
    // vue: 'Vue',
    // 'vue-router': 'VueRouter',
    // vuex: 'Vuex',
    // axios: 'axios'
  },
  css: [],
  // https://unpkg.com/browse/vue@2.6.10/
  // js: [
  // 	'js/vue.min.js',
  // 	'js/vue-router.min.js',
  // 	'js/vuex.min.js',
  // 	'js/axios.min.js'
  // ]
}

console.log('process.env.VUE_APP_API_BASE_URL', process.env.VUE_APP_API_BASE_URL)

// vue.config.js
const vueConfig = {
  configureWebpack: {
    // webpack plugins
    plugins: [
      // Ignore all locale files of moment.js
      new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
    ],
    // if prod, add externals
    externals: isProd ? assetsCDN.externals : {},
    resolve: {extensions: [".ts", ".tsx", ".js", ".json"]},
    module: {
      rules: [
        {
          test: /\.tsx?$/,
          loader: 'ts-loader',
          exclude: /node_modules/,
          options: {
            appendTsSuffixTo: [/\.vue$/],
            transpileOnly: true,
          }
        }
      ]
    }
  },
  
  chainWebpack: (config) => {
    config.resolve.alias
      .set('@$', resolve('src'))
    
    const svgRule = config.module.rule('svg')
    svgRule.uses.clear()
    svgRule
      .oneOf('inline')
      .resourceQuery(/inline/)
      .use('vue-svg-icon-loader')
      .loader('vue-svg-icon-loader')
      .end()
      .end()
      .oneOf('external')
      .use('file-loader')
      .loader('file-loader')
      .options({
        name: 'assets/[name].[hash:8].[ext]'
      })
    
    // if prod is on
    // assets require on cdn
    if (isProd) {
      config.plugin('html').tap(args => {
        args[0].cdn = assetsCDN
        return args
      })
    }
  },
  
  css: {
    loaderOptions: {
      postcss: {
        plugins: [require('tailwindcss'), require('autoprefixer')]
      },
      less: {
        modifyVars: {
          'primary-color': '#1890FF',
          'layout-color': '#1890FF',
          'border-radius-base': '2px'
        },
        // DO NOT REMOVE THIS LINE
        javascriptEnabled: true
      }
    }
  },
  
  devServer: {
    port: 8089,
    disableHostCheck:true,
    // --------------修改apiTest---------------------
    proxy: {
      '/api': {
        target: process.env.VUE_APP_API_BASE_URL,
        changeOrigin: true,
        pathRewrite: {
          '^/api': '' // 需要rewrite的,
        },
        onProxyRes(proxyRes, req, res) {
          // 真实请求网址  添加响应标头(A-Real-Url为自定义命名)，在浏览器中显示
          proxyRes.headers['A-Real-Url'] = process.env.VUE_APP_API_BASE_URL + req.url || '';
        },
      }
    }
  },
  
  // devServer: {
  // 	port: 8080,
  // 	proxy: {
  // 		'/api': {
  // 			target: process.env.VUE_APP_API_BASE_URL,
  // 			ws: false,
  // 			changeOrigin: true,
  // 			logLevel: 'debug',
  // 			pathRewrite: {
  // 				'^/api': '' // 需要rewrite的,
  // 			}
  // 		},
  // 		'/otherApi': {
  // 			target: "http://222.173.244.2/huike",
  // 			ws: false,
  // 			changeOrigin: true,
  // 			logLevel: 'debug',
  // 			pathRewrite: {
  // 				'^/otherApi': '' // 需要rewrite的,
  // 			}
  // 		}
  // 	}
  // },
  
  publicPath: './',
  outputDir: 'dist',
  assetsDir: 'static',
  
  // disable source map in production
  productionSourceMap: false,
  lintOnSave: false,
  // babel-loader no-ignore node_modules/*
  transpileDependencies: [],
  parallel: false
}

// preview.pro.loacg.com only do not use in your production;
if (process.env.VUE_APP_PREVIEW === 'true') {
  // eslint-disable-next-line no-labels
  // runtimeCompiler: true,
  // add `ThemeColorReplacer` plugin to webpack plugins
  vueConfig.configureWebpack.plugins.push(createThemeColorReplacerPlugin())
}

module.exports = vueConfig
